//
//  AYMDetailController.m
//  QuickCure
//
//  Created by kangkang on 16/5/6.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMDetailController.h"

#import "AYMTumorModel.h"


@interface AYMDetailController ()<UISearchBarDelegate>

@property(nonatomic,strong) NSArray *detailArray;

@property(nonatomic,strong) NSArray *searchArray;

@property(nonatomic,weak)   UISearchBar *search;

@property(nonatomic,weak)   UIView *warnView;

@end

NSString *detailID = @"detailcell";

@implementation AYMDetailController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupUI];
    
}

# pragma mark -
# pragma mark - setupUI
-(void)setupUI{
    
    self.navigationItem.title = @"疾病细分";
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:detailID];
    
    [self addSearchbar];
    
    [self loadData];
    
}

# pragma mark -
# pragma mark - 界面为空时显示的view
-(void)addWarnView{
    
    UIView *warnView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    
    warnView.hidden = NO;
    
    self.warnView = warnView;
    
    [self.view addSubview:warnView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jinggao"]];
    
    [warnView addSubview:imageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.text = @"请输入关键词";
    
    [warnView addSubview:label];
    
    [warnView makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.centerY.equalTo(self.view);
        
        make.width.equalTo(150);
        
        make.height.equalTo(170);
        
    }];
    
    [imageView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(warnView);
        
        make.centerX.equalTo(warnView);
        
        make.width.height.equalTo(100);
        
    }];
    
    [label makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(warnView);
        
        make.top.equalTo(imageView.bottom).offset(5);
        
        make.width.equalTo(150);
        
        make.height.equalTo(15);
        
    }];
    
    self.tableView.bounces = NO;
}

# pragma mark -
# pragma mark - 添加搜索框
-(void)addSearchbar{
    
    //添加空白的导航栏右侧按钮
       self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    
    //添加搜索框searchbar
        UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 50, 35)];
    
    search.tintColor = [UIColor lightGrayColor];
    
    [search becomeFirstResponder];
    
    self.navigationItem.titleView = search;
    
    search.delegate = self;
    
    //search.text = @"";
    
    self.search = search;
  
}

# pragma mark -
# pragma mark - //抽取加载数据的方法
-(void)loadData{

    NSArray *array = [AYMSQLiteManager queryTumorCi3_name];
    
    NSMutableArray *mutable = [NSMutableArray array];
    if (array.count != 0) {
        
        
        
        for (AYMTumorModel *model in array) {
            
            NSString *str = model.ci3_name;
            
            [mutable addObject:str];
     
        }
        
        self.searchArray = mutable.copy;
        
        [self.tableView reloadData];
        Log(@"%@",self.searchArray);
        
    }else{
        
        
        NSString *urlStr = @"http://iosapi.itcast.cn/searchCI3List.json.php";
        
        NSDictionary *params = @{
                                 @"page":@1,
                                 @"page_size":@15,
                                 @"ci1_id":@1,
                                 @"keyword":@""
                                 };
        
        
        [[AYMNetworkTool sharedManager] request:RequestMethodPost urlString:urlStr parameters:params finished:^(NSDictionary *responseObject, NSError *error) {
            
            Log(@"%@",responseObject);
            
            if (error != nil) {
                
                Log(@"cuowu");
                
                return;
                
            }
            
            Log(@"%@",responseObject);
            
            NSArray *arr = responseObject[@"data"];
            
            NSMutableArray *temArr = [NSMutableArray array];
            
            for (NSDictionary *dict in arr) {
                
                AYMTumorModel *model = [[AYMTumorModel alloc] init];
                
                [model yy_modelSetWithDictionary:dict];
                
                [temArr addObject:model];
                
                [AYMSQLiteManager insertTumor_ci3_nameWithModel:model];
                
            }
            
            self.detailArray = temArr.copy;
            
            
            for (AYMTumorModel *model in self.detailArray) {
                
                NSString *str = model.ci3_name;
                
                [mutable addObject:str];
                
            }
            self.searchArray = mutable.copy;
            
            [self.tableView reloadData];
            
        }];
    }
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     return self.searchArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailID forIndexPath:indexPath];
    
    NSString *str = self.searchArray[indexPath.row];
    
    cell.textLabel.text = str;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *str = self.searchArray[indexPath.row];
     if ([self.delegate respondsToSelector:@selector(detaildelegateWith:)]) {
        
        [self.delegate detaildelegateWith:str];
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

# pragma mark -
# pragma mark - searbar代理方法
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

   NSMutableArray *tempArr = [NSMutableArray array];
//
//    for (AYMTumorModel *model in self.searchArray) {
//        
//        [tempArr addObject:model.ci3_name];
//        
//    }
    
    NSString *string = searchText;
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];
    
//      self.searchArray = [tempArr filteredArrayUsingPredicate:pred];
    tempArr = [self.searchArray filteredArrayUsingPredicate:pred];
    
    if (tempArr.count) {
        
            self.searchArray = tempArr;
             [self.tableView reloadData];
        
    }else{
        
        return;
    }
    
//    [tempArr removeAllObjects];
   
}

@end
